Page({
  data: {
      loading: false,
      identityVerifyResult: '',
      identityInfo: null
  },
  // 身份证
  shenfenzheng() {
      this.photo("shenfenzheng");
  },
  // 银行卡
  yinhangka() {
      this.photo("yinhangka");
  },
  // 行驶证
  xingshizheng() {
      this.photo("xingshizheng");
  },
  onLoad() {
    this.getIdentityInfo();
  },
getIdentityInfo() {
    wx.cloud.callFunction({
        name: 'getIdentityInfo',
        success: (res) => {
            if (res.result.data.length > 0) {
                this.setData({
                    identityInfo: res.result.data[0]
                });
            }
        },
        fail: (err) => {
            console.error('获取实名信息失败', err);
        }
    });
  },

  // 拍照或者从相册选择要识别的照片
  photo(type) {
      wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: (res) => {
              // tempFilePath可以作为img标签的src属性显示图片
              const imgUrl = res.tempFilePaths[0];
              // 设置 loading 为 true
              this.setData({
                  loading: true
              });
              this.uploadImg(type, imgUrl);
          }
      });
  },

  // 上传图片到云存储
  uploadImg(type, imgUrl) {
      wx.cloud.uploadFile({
          cloudPath: 'ocr/' + type + '.png',
          filePath: imgUrl, // 文件路径
          success: (res) => {
              console.log("上传成功", res.fileID);
              this.getImgUrl(type, res.fileID);
          },
          fail: (err) => {
              console.log("上传失败", err);
              this.setData({
                  loading: false
              });
          }
      });
  },

  // 获取云存储里的图片url
  getImgUrl(type, imgUrl) {
      wx.cloud.getTempFileURL({
          fileList: [imgUrl],
          success: (res) => {
              const imgUrl = res.fileList[0].tempFileURL;
              console.log("获取图片url成功", imgUrl);
              this.shibie(type, imgUrl);
          },
          fail: (err) => {
              console.log("获取图片url失败", err);
              this.setData({
                  loading: false
              });
          }
      });
  },

  // 调用云函数，实现OCR识别
  shibie(type, imgUrl) {
      wx.cloud.callFunction({
          name: "ocrIdCard",
          data: {
              type: type,
              imgUrl: imgUrl
          },
          success: (res) => {
              console.log("识别成功", res);
              if (res.result.status === 'ok') {
                  const idNumber = res.result.idNumber;
                  const name = res.result.name;
                  const addr = res.result.addr;
                  const gender = res.result.gender;
                  const nationality = res.result.nationality;
                  const birth = res.result.birth;
                  this.setData({
                      identityVerifyResult: `姓名：${name}，身份证号：${idNumber}，地址：${addr}，性别：${gender}，民族：${nationality}，出生日期：${birth}`,
                      loading: false
                  });
              } else {
                  this.setData({
                      identityVerifyResult: `识别失败：${res.result.message}`,
                      loading: false
                  });
              }
          },
          fail: (res) => {
              console.log("识别失败", res);
              this.setData({
                  identityVerifyResult: '识别失败，请重试',
                  loading: false
              });
          }
      });
  }
});